package fragments;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseFragment;
import com.example.administrator.newxlmedical.GoodsDetialActivity;
import com.example.administrator.newxlmedical.InventoryActivity;
import com.example.administrator.newxlmedical.R;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import adapters.CommonTableHeadAdapter;
import beans.GoodsDetailInfoBean;
import beans.OperateGoodsDetailsBean;
import beans.RfidListBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import constants.Constants;
import dialogs.GoodsDetialsDialog;
import interfaces.MyItemClickListener;
import utils.PopupWindowUtil;


/**
 * A simple {@link Fragment} subclass.
 */
public class ReturnFragment extends BaseFragment {


    @BindView(R.id.rv_return_fragment)
    RecyclerView recyclerView;
    @BindView(R.id.fl_return_fragment_empty)
    FrameLayout flEmpty;
    Unbinder unbinder;
    @BindView(R.id.tv_return_number)
    TextView tvNumber;
    @BindView(R.id.tv_return_sure)
    TextView tvSure;

    private RfidListBean bean;
    private ReturnInterface returnInterface;
    private PopupWindow mPopupWindow;
    private CommonTableHeadAdapter adapter;
    private List<OperateGoodsDetailsBean.DataBean> list;

    public ReturnFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ReturnInterface) {
            returnInterface = (ReturnInterface) activity;
        } else {
            throw new IllegalArgumentException("activity must implements TerminalInfoInterface");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_return, container, false);
        }
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        super.initView();
        bean = (RfidListBean) getArguments().getSerializable(Constants.RETURN_FRAGMENT);
        String count = getArguments().getString("count");
        if (bean != null) {
            list = bean.getList();
            tvNumber.setText(count);
            if (list != null && list.size() > 0) {
                Collections.sort(list, new Comparator<OperateGoodsDetailsBean.DataBean>() {
                    @Override
                    public int compare(OperateGoodsDetailsBean.DataBean o1, OperateGoodsDetailsBean.DataBean o2) {
                        Collator ca = Collator.getInstance(Locale.CHINA);
                        if (ca.compare(o2.getManufacturer(),o1.getManufacturer())<0){
                            return 1;
                        }else if (ca.compare(o2.getManufacturer(),o1.getManufacturer())==0){
                            return 0;
                        }else {
                            return -1;
                        }
                    }
                });
                adapter = new CommonTableHeadAdapter(mContext, list);
                LinearLayoutManager manager = new LinearLayoutManager(mContext);
                recyclerView.setLayoutManager(manager);
                recyclerView.setAdapter(adapter);
            } else {
                showEmptyView(flEmpty);
            }
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        if (adapter != null) {
//            adapter.setOnClickListener(new CommonTableHeadAdapter.OnClickListener() {
//                @Override
//                public void onShowDetail(int position, TextView view, int xOffWidth) {
//                    mPopupWindow = PopupWindowUtil.showPopupWindow(view, view.getText().toString(), xOffWidth,mContext);
//                }
//            });
            adapter.setItemClickListener(new MyItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    OperateGoodsDetailsBean.DataBean bean = list.get(position);

                    GoodsDetailInfoBean infoBean = new GoodsDetailInfoBean();
                    infoBean.setName(bean.getGoodName());
                    infoBean.setBarCode(bean.getBarCodeList());
                    infoBean.setSpecification(bean.getSpecification());
                    infoBean.setSupplier(bean.getManufacturer());
                    infoBean.setUnit(bean.getGoodsUnit());
                    infoBean.setNumber(bean.getNumber());
                    infoBean.setType(2);
                    infoBean.setTypeStatus("放入");

                    infoBean.setTitle("放入");
                    infoBean.setTitleContent("正常物资");
                    infoBean.setTitleThree("物品详情");

                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constants.GOODS_DETIAL_INFO,infoBean);
                    Intent intent = new Intent(mContext, GoodsDetialActivity.class);
                    intent.putExtra("bundle", bundle);
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_return_sure)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_return_sure:
                returnInterface.returnSure();
                break;
        }
    }

    public interface ReturnInterface {
        void returnSure();
    }

    public PopupWindow getPopupWindow(){
        return mPopupWindow;
    }
}
